<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head th:include="/shop/include :: header"></head>
<body>
<form class="layui-form" action="" lay-filter="shop_platform_account-add-form" style="margin-top: 20px;">
    <div style="display: flex">
        <div class="layui-form-item" style="display: contents">
            <label class="layui-form-label">头像：</label>
            <div class="layui-upload-list" id="test1">
                <img class="layui-upload-img" id="demo1" style="width: 300px;height: 300px">
                <input type="hidden" name="platformHead" id="platformHead">
            </div>
        </div>
        <div style="width: 63%">
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">昵称：</label>
                <div class="layui-input-block">
                    <input id="nickName" name="nickName" placeholder="请输入昵称"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">用户名：</label>
                <div class="layui-input-block">
                    <input id="userName" name="userName" placeholder="请输入用户名"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-block">
                    <input id="passWord" name="passWord" placeholder="请输入密码"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">电话：</label>
                <div class="layui-input-block">
                    <input id="platformPhone" name="platformPhone" placeholder="请输入电话"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱：</label>
                <div class="layui-input-block">
                    <input id="email" name="email" placeholder="请输入邮箱"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
            <!--  -->
            <div class="layui-form-item">
                <label class="layui-form-label">签名：</label>
                <div class="layui-input-block">
                    <input id="signature" name="signature" placeholder="请输入签名"
                           class="layui-input" lay-verify="required" autocomplete="off">
                </div>
            </div>
        </div>
    </div>
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">所属角色：</label>
        <div class="layui-input-block">
            <select name="roleId" id="roleId">
            </select>
        </div>
    </div>
    <!--  -->
    <div class="layui-form-item">
        <label class="layui-form-label">启用状态：</label>
        <div class="layui-input-block">
            <select name="enableState" id="enableState" lay-filter="enableState">
            </select>
        </div>
    </div>
    <div class="layui-form-item" style="text-align: center">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="shop_platform_account-add">立即提交</button>
        </div>
    </div>
</form>
<div th:include="/shop/include::footer"></div>
<!-- 注意：如果你直接复制所有代码到本地，上述js路径需要改成你本地的 -->
<script>
    layui.use(['layer', 'jquery', 'form', 'laydate', "upload", 'dict'], function () {
        var $ = layui.jquery;
        var laydate = layui.laydate;
        var layer = layui.layer;
        var upload = layui.upload;
        var form = layui.form;
        var dict = layui.dict;

        dict.setSelect("enableState", "enableState");
        $.ajax({
            url: '/platform/platformrole/selectAllRole',
            type: "get",
            async: false,
            success: function (data) {
                var $html = "";
                $html += "<option value=''>请选择或搜索角色</option>";
                if (data.data != null) {
                    $.each(data.data, function (index, item) {
                        $html += "<option value='" + item.id + "'>" + item.roleName + "</option>";
                    });
                    $("#roleId").append($html);
                    //append后必须从新渲染
                    form.render('select');
                }
            }
        });


        var uploadInst = upload.render({
            elem: '#test1'
            , url: '/platform/UploadFile' //改成您自己的上传接口
            //, headers: {"token": layui.data(layui.setter.tableName)['token']}
            , before: function (obj) {
                //预读本地文件示例，不支持ie8
                obj.preview(function (index, file, result) {
                    $('#demo1').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                //如果上传失败
                if (res.status != "SUCCESS") {
                    return layer.msg('上传失败');
                }
                //上传成功
                layer.msg('上传成功');
                $("#platformHead").val(res.data)
            }
            , error: function () {
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function () {
                    uploadInst.upload();
                });
            }
        });
        //监听提交
        form.on('submit(shop_platform_account-add)', function (data) {
            $.ajax({
                url: '/platform/platformaccount/add',
                type: "post",
                data: JSON.stringify(data.field),
                contentType: 'application/json; charset=utf-8', // 很重要
                async: false,
                success: function (result) {
                    if (result.status == "SUCCESS") {
                        layer.msg(result.message, {
                            offset: '15px'
                            , icon: 1
                            , time: 1000
                        }, function () {
                            window.parent.location.reload();
                        });
                    } else {
                        layer.msg(result.message)
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>